package zcj.stu.C40;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
import org.apache.coyote.http11.Http11Nio2Protocol;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

public class C40 {
    public static void main(String[] args) throws Exception{
        //1.创建tomcat对象
        Tomcat tomcat = new Tomcat();
        tomcat.setBaseDir("tomcat");
        //2.创建项目文件夹 即docBase文件夹
        File docBase = Files.createTempDirectory("tomcat.").toFile();
        docBase.deleteOnExit();
        //3.创建tomcat项目 在tomcat中被称为context
        Context context = tomcat.addContext("", docBase.getAbsolutePath());
        //4.编程添加servlet
        WebApplicationContext webContext= readyApplication();
        context.addServletContainerInitializer(new ServletContainerInitializer() {
             @Override
             public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
                         servletContext.addServlet("baidu",new MyServlet()).addMapping("/baidu");
//                         DispatcherServlet dispatcherServlet = context1.getBean(DispatcherServlet.class);
//                         servletContext.addServlet("dispatcherServlet",dispatcherServlet).addMapping("/");
                 Map<String, ServletRegistrationBean> beans = webContext.getBeansOfType(ServletRegistrationBean.class);
                 for (ServletRegistrationBean registrationBean : beans.values()) {
                      registrationBean.onStartup(servletContext);
                 }
             }
         }, Collections.emptySet());
        //5.启动tomcat
        tomcat.start();
        //6.创建连接器设置接听端口
        Connector connector=new Connector(new Http11Nio2Protocol());
        connector.setPort(8080);
        tomcat.setConnector(connector);
    }
    public static WebApplicationContext readyApplication(){
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(WebConfig.class);
        context.refresh();
        return context;
    }
    @Configuration
    static class WebConfig{
        @Bean
        public DispatcherServlet dispatcherServlet(){
            return new DispatcherServlet();
        }
        //将dispatchServlet注册到web容器中
        @Bean
        public DispatcherServletRegistrationBean registrationBean(DispatcherServlet dispatcherServlet){
            DispatcherServletRegistrationBean registrationBean =
                    new DispatcherServletRegistrationBean(dispatcherServlet, "/");
            return registrationBean;//所有路径
        }
        @org.springframework.stereotype.Controller
        static class Controller{
            @RequestMapping("/taobao")
            public void toTaoBao(HttpServletResponse response){
                try {
                    response.sendRedirect("http://www.taobao.com");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
